package gzdev;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;

/**
 * 将生成的代码输出到文件中
 */
public class GzFile {

    private String projectPath;


    /**
     * 初始化项目目录
     */
    public GzFile() {
        String path = Objects.class.getResource("/").getPath();
        this.projectPath = path.substring(0, path.lastIndexOf("/target/classes/")) + "/src/main/java/";
    }

    /**
     * 将内容输出到指定包名位置
     *
     * @param packageName 项目的包名
     * @param fileName    文件名
     * @param content     输出的内容
     */
    protected void out(String packageName, String fileName, String content) throws IOException {
        String targetPath = projectPath + packageName.replace('.', '/');
        File dir = new File(targetPath);
        if (!dir.exists() && !dir.mkdirs())
            throw new IOException("创建目录失败：" + dir.getPath());
        File file = new File(targetPath + "/" + fileName);
        if (file.exists()){
            // 如果待生成的文件已存在
            switch (MybatisGzGenerator.MODE){
                case GzConfig.MODE_FORMAL:
                    throw new IOException(String.format("待生成的文件：%s已存在, 终止执行", file.getPath()));
                case GzConfig.MODE_ADD:
                    System.out.println("跳过生成文件:"+file.getPath());
                    return;
                case GzConfig.MODE_CONFIG:
                case GzConfig.MODE_FORCE:
                    System.out.println("已覆盖原文件：" +file.getPath());
                    if (! file.delete())
                        throw new IOException("删除文件失败:" + file.getPath());
                    break;
                default:
                    throw new IOException("mode值异常");
            }
        }
        if (! file.createNewFile())
            throw new IOException("创建文件失败：" + file.getAbsolutePath());
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
        bufferedWriter.write(content);
        bufferedWriter.flush();
        bufferedWriter.close();
    }

    /**
     * 将数据库表名转化成java的类型命名
     * @param tableName
     * @return
     */
    public String getJavaNameFromTb(String tableName){
        String preName = formatValName(tableName);
        char first = Character.isLowerCase(preName.charAt(0)) ? (char) (preName.charAt(0) - 32) : preName.charAt(0);
        return first + preName.substring(1);
    }

    /**
     * 将数据库列名转换成java变量名
     * @param columnName
     * @return
     */
    public String getValNameFromColumn(String columnName){
        return formatValName(columnName);
    }

    private String formatValName(String sqlName){
        StringBuilder no_ = new StringBuilder();
        boolean flag = false;
        for (char c : sqlName.toCharArray()){
            if (c == '_'){
                flag = true;
                continue;
            }
            if (flag){
                flag = false;
                if (Character.isLowerCase(c))
                    c -= 32;
            }
            no_.append(c);
        }
        return no_.toString();
    }

}
